package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.SupplierBean;
import com.project.mapper.ISupplierMapper;
import com.project.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class SupplierServiceImpl implements ISupplierService {
    @Autowired
    private ISupplierMapper mapper;
    @Override
    public IPage<SupplierBean> findByItem(Integer pageNO, String supplierName, String scope) {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        if (supplierName !=null){
            qw.like("s_name",supplierName);
        }
        if (scope != null){
            qw.like("s_scope",scope);
        }
        return mapper.selectPage(new Page(pageNO,3),qw);
    }

    @Override
    public SupplierBean findById(Integer supplierId) {
        return mapper.selectById(supplierId);
    }

    @Override
    public void update(Integer supplierId, String headName, String headPhone) {
        SupplierBean supplier = mapper.selectById(supplierId);
        supplier.setHeadName(headName);
        supplier.setHeadPhone(headPhone);
        mapper.updateById(supplier);
    }

    @Override
    public boolean add(SupplierBean supplier) {
        QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
        qw.eq("s_name",supplier.getSupplierName());
        if (mapper.selectCount(qw) ==0){
            mapper.insert(supplier);
            return true;
        }
        return false;
    }

    @Override
    public void addList(List<SupplierBean> supplierList){
        for (int i =0; i<supplierList.size();i++){
            SupplierBean supplier =supplierList.get(i);
            QueryWrapper<SupplierBean> qw = new QueryWrapper<>();
            qw.eq("s_name",supplier.getSupplierName());
            if (mapper.selectCount(qw)==0){
                mapper.insert(supplier);
            }
        }
    }

    @Override
    public List<SupplierBean> findAll() {
        return mapper.selectList(null);
    }
}
